package cn.sdx.ssm.controller;

import cn.sdx.ssm.pojo.Employee;
import cn.sdx.ssm.pojo.Msg;
import cn.sdx.ssm.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author sdx
 * @create 2021-05-09 2:25
 */
@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    //分页查询员工数据
    @ResponseBody
    @RequestMapping("/emps")
    public Msg getEmps(@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        //使用分页查询前需要引入 PageHelper 插件
        //在查询之前只需要调用startPage()，传入页码，以及每页的大小即可
        PageHelper.startPage(pn,5);
        List<Employee> emps = employeeService.getAllEmp();
        //使用 PageInfo 封装查询结果
        // PageInfo 封装了详细的分页信息,包括有我们查询出来的数据，需要传入连续显示的页数
        PageInfo pageInfo = new PageInfo(emps,5);
        return Msg.success().add("pageInfo",pageInfo);
    }
    /*@RequestMapping("/emps")
    public String getEmps(@RequestParam(value = "pn",defaultValue = "1") Integer pn, Model model){
        //使用分页查询前需要引入 PageHelper 插件
        //在查询之前只需要调用startPage()，传入页码，以及每页的大小即可
        PageHelper.startPage(pn,5);
        List<Employee> emps = employeeService.getAllEmp();
        //使用 PageInfo 封装查询结果
        // PageInfo 封装了详细的分页信息,包括有我们查询出来的数据，需要传入连续显示的页数
        PageInfo pageInfo = new PageInfo(emps,5);
        //将 PageInfo 对象放入请求域
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }*/

    /**
     * 校验表单数据
     */
    @ResponseBody
    @RequestMapping("isexist")
    public String isExist(Employee employee){
        boolean exist = employeeService.isExistEmp(employee);
        return exist+"";
    }
    /**
     * 添加员工
     */
    @ResponseBody
    @RequestMapping(value = "/addEmp",method = RequestMethod.POST)
    public Msg addEmp(@Valid Employee emp, BindingResult result){
        if(result.hasErrors()){
            HashMap<String, Object> map = new HashMap<>();
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                //将JSR303校验错误信息返回
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            return Msg.fail().add("errorFields",map);
        }else {
            employeeService.insertOneEmp(emp);
            return Msg.success();
        }
    }
    /**
     * 根据id获取单个员工信息
     */
    @ResponseBody
    @RequestMapping("/getEmp/{id}")
    public Msg getEmp(@PathVariable("id") Integer id){
        Employee emp = employeeService.getEmpById(id);
        return Msg.success().add("emp",emp);
    }
    /**
     * 更新员工信息
     */
    @ResponseBody
    @RequestMapping(value = "/updateEmp",method = RequestMethod.PUT)
    public Msg updateEmp(Employee emp){
        employeeService.updateEmp(emp);
        return Msg.success();
    }
    /**
     * 通用删除员工
     */
    @ResponseBody
    @RequestMapping(value = "/deleteEmp/{ids}",method = RequestMethod.DELETE)
    public Msg deleteEmp(@PathVariable("ids") String ids){
        if (ids.contains("-")){
            String[] split = ids.split("-");
            ArrayList<Integer> integers = new ArrayList<>();
            for (String s : split) {
                integers.add(Integer.parseInt(s));
            }
            employeeService.deleteAllEmp(integers);
        }else {
            employeeService.deleteEmpById(Integer.parseInt(ids));
        }
        return Msg.success();
    }

}
